﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

using Tools.Json;

using SY.Uap.Entity;
using SY.WebSite.BLL;
using SY.WebSite.Entity;

namespace SY.WebSite.Web.Handler
{
    public class SuggestHandler : InternalWebHandler, IHttpHandler
    {
        public new void ProcessRequest(HttpContext context)
        {
            base.ProcessRequest(context);

            switch (Action)
            {
                case "suggest":
                    SubmitSuggest();
                    break;
                case "query":
                    string method = RequestStringParam("method");
                    if (method == "top")
                        QueryMore();
                    else
                        Query();
                    break;
                case "todoCount":
                    TodoCount();
                    break;
                case "feedback":
                    Feedback();
                    break;
                case "myHandlerList":
                    MyHandlerList();
                    break;
            }

            context.Response.End();
        }

        public void Feedback()
        {
            int id = Tools.Web.Request.GetFormInt("id", 0);
            string feedback = Tools.Web.Request.GetFormString("feedback");

            if (id <= 0 || string.IsNullOrEmpty(feedback))
                BreakResponse();

            Suggest suggest = BizSuggest.GetByKey(id);
            if (suggest == null || suggest.Status != 1 || suggest.ToUserId != CurrentUser.UserId)
                BreakResponse();

            suggest.Feedback = Tools.StringUtil.HtmlEncode(feedback);
            suggest.Status = 2;
            int result = BizSuggest.Update(suggest);
            if (result > 0)
            {
                Response.Write(new JsonTransfer().Add("success", "success"));
                Response.End();
            }
            BreakResponse();
        }

        public void TodoCount()
        {
            JsonTransfer jt = new JsonTransfer();
            jt.Add("count", BizSuggest.GetTodoCount(CurrentUser.UserId));
            Response.Write(jt);
        }

        public void MyHandlerList()
        {
            //int pageIndex = RequestIntParam("pageIndex");
            //int pageSize = RequestIntParam("pageSize", 10);

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Suggest> list = BizSuggest.MyHandlerList(CurrentUser.UserId);

            //jt.Add("count", list.Count());
            jt.Add("list", list);

            Response.Write(jt);
        }

        public void QueryMore()
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 10);

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Hashtable> list = BizSuggest.RankList();

            jt.Add("count", list.Count());
            jt.Add("list", list.Skip(pageIndex * pageSize).Take(pageSize));

            Response.Write(jt);
        }

        public void Query()
        {
            int pageIndex = RequestIntParam("pageIndex");
            int pageSize = RequestIntParam("pageSize", 10);

            int userId = RequestIntParam("userId");

            if (userId > 0)
            {
                User user = BizUser.GetByKey(userId);
                if (user == null)
                    BreakResponse();
            }

            JsonTransfer jt = new JsonTransfer();
            IEnumerable<Suggest> list = BizSuggest.MyList(userId);

            string start = RequestStringParam("start");
            string end = RequestStringParam("end");
            
            if (!string.IsNullOrEmpty(start))
            {
                DateTime starDate;
                if (DateTime.TryParse(start, out starDate))
                {
                    list = list.Where(l => l.CreateTime >= starDate);
                }
                else
                {
                    BreakResponse();
                }
            }

            if (!string.IsNullOrEmpty(end))
            {
                DateTime endDate;
                if (DateTime.TryParse(end, out endDate))
                {
                    list = list.Where(l => l.CreateTime <= endDate);
                }
                else
                {
                    BreakResponse();
                }
            }

            string deptName = RequestStringParam("deptName");
            if (!string.IsNullOrEmpty(deptName))
            {
                IList<Hashtable> userList = BizUser.LoadUserListWithFatProperty();
                IEnumerable<int> userIds = from c in userList where c["DeptName"].ToString().Contains(deptName.Trim()) select (int)c["UserId"];
                list = list.Where(l => userIds.Contains(l.UserId));
            }

            jt.Add("count", list.Count());
            jt.Add("list", list.Skip(pageIndex * pageSize).Take(pageSize));

            Response.Write(jt);
        }

        public void SubmitSuggest()
        {
            string title = Tools.Web.Request.GetFormString("title");
            string content = Tools.Web.Request.GetFormString("content");
            string dept = Tools.Web.Request.GetFormString("dept");

            if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(content))
                BreakResponse();

            Suggest suggest = new Suggest
                                  {
                                      UserId = CurrentUser.UserId,
                                      UserName = CurrentUser.UserName,
                                      DeptName = CurrentUser.DeptName,
                                      Title = Tools.StringUtil.HtmlEncode(title),
                                      Description = Tools.StringUtil.HtmlEncode(content),
                                      RelateDeptName = Tools.StringUtil.HtmlEncode(dept)
                                  };
            int result = BizSuggest.Add(suggest);
            if (result > 0)
                Response.Write(new JsonTransfer().Add("success", result));
            else
                BreakResponse();
        }
    }
}